package Env;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import Main.Main;

/**
 *
 */

/**
 * @author dannysi
 *
 */
public class IoHandler {

    public static Environment fileParser(String fileName) throws Throwable{
//    	Main.log.fine("starting to parse");
        FileInputStream fstream = new FileInputStream(fileName);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line=br.readLine();
        StringTokenizer tok=new StringTokenizer(line," ");
        if (!(tok.nextToken().compareTo("#V")==0)){
        	Main.log.severe("error while reading the file");
            throw new Exception ("illegal file");
        }

        Graph g= new Graph(Integer.parseInt(tok.nextToken()));
        Environment e= new Environment(g);

        while ((line = br.readLine())!=null){
            tok= new StringTokenizer(line," ");
            if (tok.hasMoreElements()){
            	String firstToken=tok.nextToken();
	            if ((firstToken.compareTo("#E")==0)){
	                g.addEdge(Integer.parseInt(tok.nextToken()),Integer.parseInt(tok.nextToken()),Integer.parseInt(tok.nextToken().substring(1)),tok.nextToken().compareTo("F")==0);
	            }else if ((firstToken.compareTo("#V")==0)){
	                e.addCar(Integer.parseInt(tok.nextToken()),Integer.parseInt(tok.nextToken()),Float.parseFloat(tok.nextToken()));
	            }else {
	            	Main.log.severe("error while reading the file");
	            	throw (new Exception ("illegal file line "+line+ "---"+tok.nextToken()));
	            	}
            }
            
        }
        Main.log.fine("finished parsing successfully");
        return e;

    }

}